package com.suixun.jxt.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.suixun.entity.jxt.Result0;
import com.suixun.entity.jxt.Teacher0;
import com.suixun.jxt.service.JxtTeacherService;
import com.suixun.service.Constants;
import com.suixun.util.Constant;
import com.suixun.util.HttpUtil;
import com.suixun.util.JWTHelper;

import net.sf.json.JSONObject;

@Service
public class JxtTeacherServiceImpl implements JxtTeacherService {
	
	private final static Logger logger = LoggerFactory.getLogger(JxtTeacherServiceImpl.class);

	@Override
	public Teacher0 getTeacherInfoFromJxtBytel(Integer peopleid, String peopletel, String peoplename) throws Exception {
		if (peoplename == null) {
			peoplename = "";
		}
		JWTHelper jWTHelper = new JWTHelper();
		String token = jWTHelper.createJWT(String.valueOf(peopleid), peopletel, peoplename, 0l, "teather");
		StringBuilder sb = new StringBuilder();
		sb.append("method=ajaxSuiXungetTeacherInfo");
		sb.append("&teachertel=");
		sb.append(peopletel);
		sb.append("&token=");
		sb.append(token);
		Teacher0 teacher2 = null;
		// JxtClass teacher2=null;
		try {
			logger.info(Constants.getJxtPath() + Constant.API.GETTEACHER+ "?" + sb.toString());
			String result = HttpUtil.sendPost(Constants.getJxtPath() + Constant.API.GETTEACHER, sb.toString(), false);
			logger.info(result);
			Gson gson = new Gson();

			Result0 mapresult = gson.fromJson(result, Result0.class);
			if (mapresult == null) {
				throw new Exception();
			}
			JSONObject teacherJson = (JSONObject) mapresult.getData();
			if (teacherJson != null) {
				teacher2 = (Teacher0) JSONObject.toBean(teacherJson, Teacher0.class);
			}
			if (teacher2 == null) {
				throw new Exception();
			}
		} catch (Exception e) {
			throw new Exception();
		}
		return teacher2;
	}

}
